<?php

namespace App\Service;

use App\Entity\CronTabTask;
use App\Entity\Money;
use App\Entity\MoneyCashLog;
use App\Entity\MoneyFreezeLog;
use App\Entity\MoneyLog;
use App\Entity\MoneyPayOrder;
use App\Entity\MoneyType;
use App\Entity\PlatIncome;
use App\Entity\PlatIncomeLog;
use App\Entity\User;
use App\Repository\ConfigRepository;
use App\Repository\CronTabTaskRepository;
use App\Repository\MoneyConfigRepository;
use App\Repository\MoneyFreezeLogRepository;
use App\Repository\MoneyLogRepository;
use App\Repository\MoneyLogTypeRepository;
use App\Repository\MoneyRepository;
use App\Repository\MoneyStaticRepository;
use App\Repository\MoneyTypeRepository;
use App\Repository\PlatIncomeLogRepository;
use App\Repository\PlatIncomeRepository;
use App\Repository\UserRepository;
use App\Service\Wechat\WechatPay;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use function Doctrine\ORM\QueryBuilder;

class MoneyService extends ServiceEntityRepository
{

    //资金账户设置
    const __MONEY_XIAN_JIN__ = 1;
    //日志类型设置


    public $userRepository;
    public $moneyRepository;
    public $moneyLogRepository;
    public $moneyTypeRepository;
    public $moneyConfigRepository;
    public $moneyStaticRepository;
    public $moneyLogTypeRepository;
    public $platIncomeRepository;
    public $platIncomeLogRepository;
    public $configRepository;
    public $cronTabTaskRepository;
    public $moneyFreezeLogRepository;

    public function __construct(
        ManagerRegistry $registry
        , UserRepository $userRepository
        , MoneyRepository $moneyRepository
        , MoneyLogRepository $moneyLogRepository
        , MoneyTypeRepository $moneyTypeRepository
        , MoneyConfigRepository $moneyConfigRepository
        , MoneyStaticRepository $moneyStaticRepository
        , MoneyLogTypeRepository $moneyLogTypeRepository
        , ConfigRepository $configRepository
        , MoneyFreezeLogRepository $moneyFreezeLogRepository
    )
    {
        parent::__construct($registry, Money::class);
        $this->userRepository = $userRepository;
        $this->moneyRepository = $moneyRepository;
        $this->moneyLogRepository = $moneyLogRepository;
        $this->moneyTypeRepository = $moneyTypeRepository;
        $this->moneyConfigRepository = $moneyConfigRepository;
        $this->moneyStaticRepository = $moneyStaticRepository;
        $this->moneyLogTypeRepository = $moneyLogTypeRepository;
        $this->configRepository = $configRepository;
        $this->moneyFreezeLogRepository = $moneyFreezeLogRepository;
    }


    public function mod(User $user, $money, $moneyTypeId, $logTypeId, $tips = "", $param = [])
    {//通用资金添加，增加资金的时候变动余额，增加记录，改变统计信息
        $this->_em->beginTransaction();
        try {
            $moneyType = $this->moneyTypeRepository->find($moneyTypeId);
            $logType = $this->moneyLogTypeRepository->findOneBy(array("code" => $logTypeId));
            $userMoney = $this->moneyRepository->getUserMoneyByType($user, $moneyType);
            $curMoney = $userMoney->getMoney();
            //变更余额
            $userMoney->setMoney($curMoney + $money);
            $this->_em->persist($userMoney);

            //变更数据统计
            $userStatic = $this->moneyStaticRepository->getUserMoneyStaticByType($user, $moneyType, $logType);
            $curStaticMoney = $userStatic->getMoney();
            $userStatic->setMoney($curStaticMoney + $money);
            $this->_em->persist($userStatic);

            //增加流水记录
            $MoneyLog = new MoneyLog();
            $MoneyLog->setMoney($money)
                ->setUser($user)
                ->setMoneyType($moneyType)
                ->setLogType($logType)
                ->setCreateAt(new \DateTime())
                ->setTips($tips)->setParam($param);
            $this->_em->persist($MoneyLog);
            $this->_em->flush();
        } catch (\Exception $exception) {
            $this->_em->rollback();
            throw $exception;
        }
        $this->_em->commit();
    }

    public function getUserMoney(User $user)
    {
        $money = [];
        return $money;
    }




    public function userCash(User $user, $money)
    {
        //修改资金变动
        self::mod($user, $money * -1, self::__MONEY_XIAN_JIN__, 201, "余额提现", []);

        $card = $user->getMoneyCards()->getValues()[0] ?? null;
        //添加提现记录
        $MoneyCashLog = new MoneyCashLog();
        $MoneyCashLog->setMoney($money)
            ->setUser($user)
            ->setStatus(0)
            ->setCreateAt(new \DateTime())
            ->setMessage("")
            ->setCard($card);
        $this->_em->persist($MoneyCashLog);

        $this->_em->flush();
    }

    public function userCashFail(MoneyCashLog $MoneyCashLog, $message = "提现被拒")
    {
        $user = $MoneyCashLog->getUser();
        $money = $MoneyCashLog->getMoney();
        //修改资金变动
        self::mod($user, $money, self::__MONEY_XIAN_JIN__, 109, "提现失败返回", []);

        //修改提现记录
        $MoneyCashLog->setDealAt(new \DateTime())
            ->setMessage($message)
            ->setStatus(2);
        $this->_em->persist($MoneyCashLog);

        $this->_em->flush();
    }

    public function getUserLog(User $user, $moneyTypeId, $logTypeCode, $page = 1, $limit = 10)
    {
        $moneyType = $this->moneyTypeRepository->find($moneyTypeId);
        $logType = $this->moneyLogTypeRepository->findOneBy(array("code" => $logTypeCode));
//        $qb = $this->moneyLogRepository->createQueryBuilder("A");
        $condition = array(
            "user" => $user,
            "moneyType" => $moneyType
        );
        if ($logTypeCode) {
            $condition["logType"] = $logType;
        }
        $data = $this->moneyLogRepository->findBy(
            $condition,
            array("id" => "DESC")
            , $limit, ($page - 1) * $limit
        );
        return $data;
    }
}